package com.rpp.session;

import com.rpp.enums.SqlCommandType;
import com.rpp.pojo.Configuration;
import com.rpp.pojo.MapedStatement;

import java.lang.reflect.*;
import java.util.List;

public class DefaultSqlSession implements SqlSession {

    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <E> List<E> selectList(String statementId, Object... parameter) throws Exception {

        MapedStatement mapedStatement = configuration.getMapedStatementMap().get(statementId);
        Executor executor = new SimpleExecutor();
        List<Object> query = executor.query(configuration, mapedStatement, parameter);
        return (List<E>) query;
    }

    @Override
    public <T> T selectOne(String statementId, Object... parameter) throws Exception {

        List<Object> objects = selectList(statementId, parameter);
        if (null == objects || objects.size() == 0) {
            throw new RuntimeException("未查询到数据");
        }
        if (objects.size() != 1) {
            throw new RuntimeException("查询结果多于一条");
        }
        return (T) objects.get(0);
    }

    @Override
    public int insert(String statementId, Object... parameter) {
        return update(statementId, parameter);
    }

    @Override
    public int update(String statementId, Object... parameter) {
        MapedStatement mapedStatement = configuration.getMapedStatementMap().get(statementId);
        Executor executor = new SimpleExecutor();
        try {
            return executor.update(configuration, mapedStatement, parameter);
        } catch (Exception e) {
            throw new RuntimeException("操作数据库异常:" + e, e);
        }
    }

    @Override
    public int delete(String statementId, Object... parameter) {
        return update(statementId, parameter);
    }

    //生成代理类
    @Override
    public <T> T getMapper(Class<?> clss) {

        T obj = (T)Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{clss}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                //方法名称
                String methodName = method.getName();
                //className:nameSpace
                String className = method.getDeclaringClass().getName();
                //statementId
                String key = className + "." + methodName;

                MapedStatement mapedStatement = configuration.getMapedStatementMap().get(key);
                SqlCommandType sqlCommandType = mapedStatement.getSqlCommandType();
                switch (sqlCommandType) {
                    case SELECT: {
                        Type genericReturnType = method.getGenericReturnType();
                        //判断是否实现泛型类型参数化
                        if (genericReturnType instanceof ParameterizedType) {
                            return selectList(key, args);
                        }
                        return selectOne(key, args);
                    }
                    case INSERT: {
                        return insert(key, args);
                    }
                    case UPDATE: {
                        return update(key, args);
                    }
                    case DELETE: {
                        return delete(key, args);
                    }
                    default:
                        return null;
                }
            }
        });
        return  obj;
    }
}
